﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShoppingApp
{
    public partial class Settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //When site is loaded for the first time
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["AcceptCookies"] == null)
                {
                    //Create test cookie
                    Response.Cookies["TestCookie"].Value = "ok";
                    Response.Cookies["TestCookie"].Expires = DateTime.Now.AddMinutes(1);
                    Response.Redirect("~/TestCookie.aspx?redirect=" + Server.UrlEncode(Request.Url.ToString()));
                }
                else
                {
                    if (Request.QueryString["AcceptCookies"] == "yes")
                    {
                        ControlsCookies(true);
                        ReadDataFromCookie();
                    }
                    else
                    {
                        ControlsCookies(false);
                    }
                }
            }
            else
            {
                //In PostBack
                System.Diagnostics.Debug.WriteLine("CheckBox in postback is: " + CheckBox1.Checked.ToString());
                Response.Cookies["shoppingApp"].Value = CheckBox1.Checked.ToString();
                Response.Cookies["shoppingApp"].Expires = DateTime.Now.AddDays(1);
            }

            //No matter if this is PostBack or !PostBack
            SiteMaster myPage = Page.Master as SiteMaster;
            myPage.ValidationMenuItem.Enabled = CheckBox1.Checked;
        }

        /// <summary>
        /// Sets state of controls depending on decision if browser accepts cookies
        /// </summary>
        /// <param name="acceptsCookies">If browser accepts cookies - true</param>
        protected void ControlsCookies(bool acceptsCookies)
        {
            if (acceptsCookies == true)
            {
                CheckBox1.Visible = true;
                Button1.Visible = true;
                Label1.Visible = false;
            }
            else
            {
                CheckBox1.Visible = false;
                Button1.Visible = false;

                //Show label to user - your browser doesn't accept cookies
                Label1.Visible = true;
            }
        }

        protected void ReadDataFromCookie()
        {
            if (Request.Cookies["shoppingApp"] != null)
            {
                try
                {
                    //bool decision = bool.Parse(Request.Cookies["shoppingApp"].Value);
                    //System.Diagnostics.Debug.WriteLine("Decision is " + decision.ToString());
                    //CheckBox1.Checked = decision;
                    CheckBox1.Checked = bool.Parse(Request.Cookies["shoppingApp"].Value);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception caught after reading a cookie " + ex.Message);
                }
            }
        }
                
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Changes here won't affect page look            
            Label1.Text = "Button clicked";
        }

    }
}